package com.flyoil.petromp.ui.activity.activity_login;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cnpc.fyutils.EditText_util;
import com.cnpc.fyutils.NotificationsUtils;
import com.cnpc.fyutils.ToastUtils;
import com.flyoil.petromp.Constant.ConstantNetworkCode;
import com.flyoil.petromp.R;
import com.flyoil.petromp.apiservice.HttpModel;
import com.flyoil.petromp.apiservice.OnObserverRetrofitResetCallBack;
import com.flyoil.petromp.base.BaseActivity;
import com.flyoil.petromp.base.BaseEntity;
import com.flyoil.petromp.entity.entity_login.LoginEntity;
import com.flyoil.petromp.ui.activity.activity_home.HomeActivity;
import com.flyoil.petromp.utils.Dialog.Dialog_util;
import com.flyoil.petromp.utils.HintConstants;
import com.flyoil.petromp.utils.MoccaPreferences;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录界面
 */
public class LoginActivity extends BaseActivity {
    private TextView tv_confirm, tv_versionName, tv_appName;
    private ImageView img_logo, img_backgroud;
    private EditText edt_user, edt_pwd;

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initViews() {
        super.initViews();
        httpModel = new HttpModel();
        tv_confirm = $(R.id.tv_login_confirm);
        tv_versionName = $(R.id.tv_login_app_version_name);
        tv_appName = $(R.id.tv_login_app_name);
        img_logo = $(R.id.img_login_app_logo);
        img_backgroud = $(R.id.img_login_backgroud);
        edt_user = $(R.id.edt_login_user);
        edt_pwd = $(R.id.edt_login_pwd);
        EditText_util.setedittext_type_numberletters(edt_user);
        EditText_util.setedittext_type_numberletters(edt_pwd);
        tv_versionName.setText("V" + getAppVersionName(mContext));
        edt_user.setText(MoccaPreferences.login_user.get());
        edt_pwd.setText(MoccaPreferences.login_pwd.get());
        if (NotificationsUtils.isNotificationEnabled(LoginActivity.this) == false) {
            NotificationsUtils.showAppDetailSettingDialog(LoginActivity.this);
        }
    }
    //返回键的监听,登录界面点按返回键，不执行切换界面的动画效果
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        tv_confirm.setOnClickListener(onClickListener);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tv_login_confirm:
                    if (edt_user.getText().toString().length() == 0) {
                        ToastUtils.show_toast(HintConstants.LOGIN_USER_NULL);
                        return;
                    }
                    if (edt_pwd.getText().toString().length() == 0) {
                        ToastUtils.show_toast(HintConstants.LOGIN_PWD_NULL);
                        return;
                    }
                    login_server();
                    break;
            }
        }
    };


    /**
     * 获取当前版本号
     */
    private String getAppVersionName(Context context) {
        String versionName = "";
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo("com.flyoil.petromp", 0);
            versionName = packageInfo.versionName;
            if (TextUtils.isEmpty(versionName)) {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionName;
    }

    /**
     * 登录方法
     */
    private void login_server() {
        Dialog_util.start_NetworkRequests_diolog(mContext);
        EditText_util.hideKeyboard(mContext);
        Map<String, Object> map = new HashMap<>();
        map.put("username", edt_user.getText().toString().trim());
        map.put("password", edt_pwd.getText().toString().trim());
        httpModel.login_server(map, new OnObserverRetrofitResetCallBack<BaseEntity>() {

            @Override
            public void onSuccess(BaseEntity model) {
                Dialog_util.close_NetworkRequests_diolog();
                LoginEntity entity = null;
                if (model instanceof LoginEntity) {
                    entity = (LoginEntity) model;
                }
                if (entity != null && entity.getCode() == ConstantNetworkCode.SuccedCode) {
                    MoccaPreferences.login_user.put(edt_user.getText().toString());
                    MoccaPreferences.login_pwd.put(edt_pwd.getText().toString());
                    MoccaPreferences.sessionId.put(entity.getData().getSessionId());
                    MoccaPreferences.user_avatar.put(entity.getData().getAvatar());
                    MoccaPreferences.user_signature.put(entity.getData().getSignature());
                    MoccaPreferences.signature_seted.put(entity.getData().isHasSignature());
                    MoccaPreferences.companyId.put(entity.getData().getCompanyId());
                    MoccaPreferences.user_login.put(true);
                    startActivity(new Intent(mContext, HomeActivity.class));
                    finish();
                } else {
                    ToastUtils.show_toast(model.getMessage());
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }
}
